package cn.com.gridinfo.sooc.core.course.pay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.squareup.picasso.Picasso;

import org.apache.http.message.BasicNameValuePair;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.course.pay.common.CodeConst;
import cn.com.gridinfo.sooc.core.course.pay.dao.PayDao;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 购买课程
 * Created by yingchunli on 2016/8/9.
 */
public class BuyCourseActivity extends BaseSoocActivity {

    @InjectView(R.id.kcfm)
    ImageView kcfm;//课程封面
    @InjectView(R.id.kcmc)
    TextView kcmc;//课程名称
    @InjectView(R.id.kcls)
    TextView kcls;//课程老师
    @InjectView(R.id.price)
    TextView price;//课程价钱
    @InjectView(R.id.summit)
    Button summit;
    private String kcid, intent_kcfm;//课程id 课程封面
    PayDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_buycourse);
        ButterKnife.inject(this);
        dao = new PayDao(this);
        //显示课程信息
        intent_kcfm = getIntent().getStringExtra("kcfm");
        Picasso.with(this).load(intent_kcfm).resize(getResources().getDimensionPixelSize(R.dimen.fengmian_width_in_mine),
                getResources().getDimensionPixelSize(R.dimen.fengmian_height_in_mine)).placeholder(R.mipmap.course_placeholder)
                .error(R.mipmap.course_placeholder).into(kcfm);
        kcid = getIntent().hasExtra("kcid") ? getIntent().getStringExtra("kcid") : "";
        String intent_kcmc = getIntent().hasExtra("kcmc") ? getIntent().getStringExtra("kcmc") : "";
        String intent_price = getIntent().hasExtra("price") ? getIntent().getStringExtra("price") : "";
        String intent_kcls = getIntent().hasExtra("kcls") ? getIntent().getStringExtra("kcls") : "";
        kcmc.setText(intent_kcmc);
        kcls.setText(intent_kcls);
        price.setText("￥" + intent_price);

        if (intent_price.equals("0.00")) {
            //0元课程
            summit.setText("确认提交");
        } else {
            summit.setText("提交订单");
        }
    }

    /**
     * 确认提交按钮的点击
     */
    @OnClick(R.id.summit)
    void onClick() {
        //调用生成订单的接口
        summit.setEnabled(false);
        dao.payOrder(kcid);
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                //生成订单
                if (requestCode == CodeConst.PAY_ORDER) {
                    summit.setEnabled(true);
                    //0元课程
                    if (summit.getText().toString().trim().equals("确认提交")) {
                        //进入支付结果页面
                        IntentUtil.intentSlidIn(this, PayResultActivity.class, 00, new BasicNameValuePair("from", "buyCourse"),
                                new BasicNameValuePair("kcid", kcid), new BasicNameValuePair("kcfm", intent_kcfm));
                        noAnimfinish();
                    } else {
                        //跳转到去支付页面
                        Intent intent = new Intent(this, PayOrderActivity.class);
                        intent.putExtra("kcid", kcid);
                        intent.putExtra("kcmc", kcmc.getText().toString().trim());
                        intent.putExtra("price", price.getText().toString().trim());
                        intent.putExtra("kcfm", intent_kcfm);
                        intent.putExtra("from","buyCourse");
                        startActivity(intent);
                        AnimUtil.intentSlidIn(this);
                    }
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "数据请求异常");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "购买课程";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        finish();
    }
}
